package cn.datawin.cotask;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;

import org.apache.commons.logging.LogFactory;

import cn.datawin.spider.CCMan;
import cn.datawin.task.Config;
import cn.datawin.task.PrepareTask;
import cn.datawin.task.Statistic;
import cn.datawin.task.dao.DbUtil;
import cn.datawin.task.dao.Resource;
import cn.datawin.task.util.PropertiesUtil;

public class MainRun {
	
	public static void init() throws IOException{
		LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog");
		java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
		
		Resource resource = new Resource("/client.properties");
		Properties props = PropertiesUtil.loadProperties(resource);
		Config.apps.putAll(PropertiesUtil.toMap(props));
	}

	
	public static void main(String[] args) throws IOException {
		init();
		DbUtil.connect();
		
		/**
		 * 用于登录用户流程
		 * 登录, 或者登录抓取rule 规则模式
		 */
		TaskPoll taskPoll = new TaskPoll();
		taskPoll.setState("0");
		taskPoll.setWorkbeach(Config.apps.get("workbeach"));
		taskPoll.start();
		
		ExecTaskPoll execTaskPoll = new ExecTaskPoll();
		execTaskPoll.setPageQueue(TaskPoll.queue);
		execTaskPoll.thread(10, 100, 100).start();
		
		
		/**
		 * 用于抓取数据流程
		 * 1. 登录模式
		 * 2. 非登录模式
		 */
		PrepareTask pTask = new PrepareTask(Config.apps.get("workbeach"),"0");
		Statistic statistic=Statistic.getInstance();
		pTask.setStatistic(statistic);
		pTask.start();
		statistic.setPrepareTask(pTask);
		CCMan cc = new CCMan();
		cc.setPageQueue(PrepareTask.pageQueue);
		cc.thread(10, 10, 100).start();
		statistic.setCcMan(cc);
		
	}
}
